package com.minimalist.common.feign.fallback.tc;

import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.tc.entity.vo.order.TcOrderQueryVO;
import com.minimalist.common.entity.tc.entity.vo.order.TcOrderVO;
import com.minimalist.common.feign.client.tc.TcOrderFeignClient;
import com.minimalist.common.feign.config.BaseFallbackFactory;
import com.minimalist.common.response.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 车牌交易订单 Feign 客户端降级工厂
 * 使用FallbackFactory可以获取到异常信息，便于问题排查
 *
 * @author minimalist
 */
@Component
public class TcOrderFeignClientFallbackFactory extends BaseFallbackFactory<TcOrderFeignClient> {
    private static final Logger log = LoggerFactory.getLogger(TcOrderFeignClientFallbackFactory.class);


    @Override
    public TcOrderFeignClient create(Throwable cause) {
        return new TcOrderFeignClient() {
            @Override
            public R<?> addOrder(TcOrderVO orderVO) {
                logServiceUnavailable("创建订单", orderVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), "服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> updateOrder(TcOrderVO orderVO) {
                logServiceUnavailable("更新订单", orderVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), "服务不可用: " + cause.getMessage());
            }

            @Override
            public R<TcOrderVO> getOrder(Long orderId) {
                logServiceUnavailable("获取订单详情", orderId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), "服务不可用: " + cause.getMessage());
            }

            @Override
            public R<Map<String, Object>> importExcel(MultipartFile file) {
                logServiceUnavailable("导入订单", file != null ? file.getOriginalFilename() : "null", cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), "服务不可用: " + cause.getMessage());
            }

            @Override
            public R<PageResp<TcOrderVO>> getPageOrderList(TcOrderQueryVO queryVO) {
                logServiceUnavailable("分页查询订单列表", queryVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), "服务不可用: " + cause.getMessage());
            }
        };
    }
}
